package com.test.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

public class FileUtil {

    public static String getFileBase64(byte[] bytes) {
        return Base64Utils.encode(bytes);
    }

    public static String getFileBase64(String filePath) throws Exception{
        byte[] bytes = getFileBytes(filePath);
        if (bytes == null) {
            return "";
        } else {
            return getFileBase64(bytes);
        }
    }

    public static byte[] getFileBytes(String filePath) throws Exception{
        File file = new File(filePath);
        if (!file.exists()) {
            return null;
        }
        FileInputStream in = new FileInputStream(file);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buff = new byte[4096];
        int len = 0;
        while((len = in.read(buff)) > 0) {
            out.write(buff, 0, len);
        }
        return out.toByteArray();
    }
}
